package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.FocusUser;
import com.tanhua.service.FocusUserApi;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

/**
 * @author げっしょく
 * @date 2021/11/2
 */
@DubboService
public class FocusUserApiImpl implements FocusUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;
    //关注用户
    @Override
    public void save(FocusUser focusUser) {
        Criteria criteria = Criteria.where("userId").is(focusUser.getUserId())
                .and("followUserId").is(focusUser.getFollowUserId());
        Query query = Query.query(criteria);
        FocusUser one = mongoTemplate.findOne(query, FocusUser.class);
        if (one == null) {
            mongoTemplate.save(focusUser);
        }
    }
    //取消关注
    @Override
    public void delete(FocusUser focusUser) {
        Criteria criteria = Criteria.where("userId").is(focusUser.getUserId())
                .and("followUserId").is(focusUser.getFollowUserId());
        Query query = Query.query(criteria);
        FocusUser one = mongoTemplate.findOne(query, FocusUser.class);
        if (one != null) {
            mongoTemplate.remove(query, FocusUser.class);
        }
    }
}
